package com.cys.dm.server;

import com.cys.dm.client.user.DmUserClient;
import com.cys.dm.entity.user.DmUser;
import com.cys.dm.exceptions.DmException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@Service
public class DmUserServer {

    @Autowired
    private DmUserClient dmUserClient;

    /*手机号登录*/
    public DmUser login(String phone,String password){
        return dmUserClient.findByPhoneAndPassword(phone,password);
    }

    /*验证码登录*/
    public DmUser loginByVerification(String phone){
        return dmUserClient.findByVerification(phone);
    }

    /*存储用户信息，用户注册*/
    public Integer register(HashMap<String, String> map){
        DmUser dmUserflg = dmUserClient.findByPhone(map.get("phone"));
        if (dmUserflg!=null){
            throw new DmException("该手机号已注册","1003");
        }

        DmUser dmUser = new DmUser();
        dmUser.setPhone(map.get("phone"));
        dmUser.setPassword(map.get("password"));

        return dmUserClient.saveUser(dmUser);
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    public String uploadImage(MultipartFile file) {
        String filename = file.getOriginalFilename();
        File destFile = new File("F:\\学习库\\邯郸大学\\SpringBoot\\springboot-dm整合\\解压测试\\dmCode\\static\\img\\photo",filename);
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "../../../../static/img/photo"+filename;
    }


}
